package weekMatch.one_344;

import java.util.Arrays;

public class LeeCode_03 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(colorTheArray(4, new int[][]{{0, 2}, {1, 2}, {3, 1}, {1, 1}, {2, 1}})));
    }
    private static int[] colorTheArray(int n, int[][] queries) {
        int[] arr = new int[n];
        int m = queries.length;
        int[] ans = new int[m];
        int res = 0;
        int count = 0;
        for (int i = 0; i < m; i++) {
            int[] query = queries[i];
            int index = query[0], change = 0;
            if (index - 1 >= 0 && arr[index] == arr[index - 1] && arr[index] != 0 && query[1] != arr[index]){
                change--;
            }
            if (index + 1 < n && arr[index] == arr[index + 1] && arr[index] != 0 && query[1] != arr[index]){
                change--;
            }
            if (index - 1 >= 0 && arr[index] != arr[index - 1] && query[1] == arr[index - 1] && arr[index - 1] != 0){
                change++;
            }
            if (index + 1 < n && arr[index] != arr[index + 1] && query[1] == arr[index + 1] && arr[index + 1] != 0){
                change++;
            }
            res += change;
            ans[i] = res;
            arr[index] = query[1];
        }
        return ans;
    }
}
